version 13
set more off
global maindir "/home/`c(username)'/Dropbox/Research/Nazis"

use "$maindir/Data/master.dta", clear
eststo clear

* some tex stuff for table output
global texcode "prehead(\begin{table}`=char(13)'`=char(9)'\centering`=char(13)'`=char(9)'\caption{@title}`=char(13)'`=char(9)'\label{}`=char(13)'`=char(9)'\footnotesize`=char(13)'`=char(9)'\begin{threeparttable}`=char(13)'`=char(9)'`=char(9)'\begin{tabular}{l*{@M}{S}}`=char(13)'\toprule) posthead(\cmidrule(lr{.75em}){2-@M}) prefoot(\cmidrule(lr{.75em}){2-@M}) postfoot(\bottomrule`=char(13)'`=char(9)'`=char(9)'\end{tabular}`=char(13)'`=char(9)'\begin{tablenotes}[para,flushleft]`=char(13)'`=char(9)'`=char(9)'\item Note: @note@starlegend.`=char(13)'`=char(9)'\end{tablenotes}`=char(13)'`=char(9)'\end{threeparttable}`=char(13)'\end{table}) substitute(_ :)"

#delimit ;
global varlabs "
  _cons     "Constant"
  b1:_cons  "subdocmel1psh"
  b2:_cons  "subdocmel1pshxinfra"
";
#delimit cr


******************** MAIN TABLE: OLS, IV x 3 **********************************
// TABLE 3

* OLS
eststo olx_infoaratel1lebgeb:      xi: xtreg infoaratel1lebgeb             i.jahr subdocmel1psh, fe vce(cluster id)
eststo olx_mdarmu1jratel1lebgeboa: xi: xtreg mdarmu1jratel1lebgeboa        i.jahr subdocmel1psh, fe vce(cluster id)
eststo olx_angeblsratel1lebgeboa:  xi: xtreg angeblsfruehratel1lebgeboa    i.jahr subdocmel1psh, fe vce(cluster id)
eststo olx_stillboaratel1geb:      xi: xtreg stillbadjoaratel1geb          i.jahr subdocmel1psh, fe vce(cluster id)
estout using "$maindir/Results/Tables/main.tex", style(tex) replace ///
    keep(subdoc*) ///
    cells(b(star fmt(3)) se(par fmt(3)) p(par([ ]) fmt(3))) ///
    stats(widstat ymean N_clust N, fmt(2 2 0 0)) ///
    starlevels(* 0.1 ** 0.05 *** 0.01) ///
    $texcode
eststo clear

* IV: EMIGRATION
eststo ive_infoaratel1lebgeb:      xi: xtivreg2 infoaratel1lebgeb          i.jahr (subdocmel1psh = emigration), first fe cluster(id)
eststo ive_mdarmu1jratel1lebgeboa: xi: xtivreg2 mdarmu1jratel1lebgeboa     i.jahr (subdocmel1psh = emigration), first fe cluster(id)
eststo ive_angeblsratel1lebgeboa:  xi: xtivreg2 angeblsfruehratel1lebgeboa i.jahr (subdocmel1psh = emigration), first fe cluster(id)
eststo ive_stillboaratel1geb:      xi: xtivreg2 stillbadjoaratel1geb       i.jahr (subdocmel1psh = emigration), first fe cluster(id)
estout using "$maindir/Results/Tables/main.tex", style(tex) append ///
    keep(subdoc*) ///
    cells(b(star fmt(3)) se(par fmt(3)) p(par([ ]) fmt(3))) ///
    stats(widstat ymean N_clust N, fmt(2 2 0 0)) ///
    starlevels(* 0.1 ** 0.05 *** 0.01)
eststo clear

* IV: JEWISH PHYSICIANS
eststo ivj_infoaratel1lebgeb:      xi: xtivreg2 infoaratel1lebgeb          i.jahr (subdocmel1psh = jewdocxpost), first fe cluster(id)
eststo ivj_mdarmu1jratel1lebgeboa: xi: xtivreg2 mdarmu1jratel1lebgeboa     i.jahr (subdocmel1psh = jewdocxpost), first fe cluster(id)
eststo ivj_angeblsratel1lebgeboa:  xi: xtivreg2 angeblsfruehratel1lebgeboa i.jahr (subdocmel1psh = jewdocxpost), first fe cluster(id)
eststo ivj_stillboaratel1geb:      xi: xtivreg2 stillbadjoaratel1geb       i.jahr (subdocmel1psh = jewdocxpost), first fe cluster(id)
estout using "$maindir/Results/Tables/main.tex", style(tex) append ///
    keep(subdoc*) ///
    cells(b(star fmt(3)) se(par fmt(3)) p(par([ ]) fmt(3))) ///
    stats(widstat ymean N_clust N, fmt(2 2 0 0)) ///
    starlevels(* 0.1 ** 0.05 *** 0.01)
eststo clear

* IV: JEWISH POPULATION 1933
eststo ivc_infoaratel1lebgeb:      xi: xtivreg2 infoaratel1lebgeb          i.jahr (subdocmel1psh = c33judenxpost), first fe cluster(id)
eststo ivc_mdarmu1jratel1lebgeboa: xi: xtivreg2 mdarmu1jratel1lebgeboa     i.jahr (subdocmel1psh = c33judenxpost), first fe cluster(id)
eststo ivc_angeblsratel1lebgeboa:  xi: xtivreg2 angeblsfruehratel1lebgeboa i.jahr (subdocmel1psh = c33judenxpost), first fe cluster(id)
eststo ivc_stillboaratel1geb:      xi: xtivreg2 stillbadjoaratel1geb       i.jahr (subdocmel1psh = c33judenxpost), first fe cluster(id)
estout using "$maindir/Results/Tables/main.tex", style(tex) append ///
    keep(subdoc*) ///
    cells(b(star fmt(3)) se(par fmt(3)) p(par([ ]) fmt(3))) ///
    stats(widstat ymean N_clust N, fmt(2 2 0 0)) ///
    starlevels(* 0.1 ** 0.05 *** 0.01)
eststo clear


/* =============================== FIRST STAGE ============================== */

// TABLE 2

preserve
eststo clear

replace emigration = emigration/1000
replace jewdocxpost = jewdocxpost/1000
replace c33judenxpost = c33judenxpost/1000
eststo emfsr: xi: xtreg subdocmel1psh     i.jahr emigration, fe cluster(id)
eststo jdfsr: xi: xtreg    subdocmel1psh     i.jahr jewdocxpost, fe cluster(id)
eststo cjfsr: xi: xtreg    subdocmel1psh     i.jahr c33judenxpost, fe cluster(id)

restore


/* ============= SUPPLEMENTARY RESULTS: DISEASES, IV: EMIGRATION ============ */

// TABLE 4

eststo ive_todu1joal1psh     : xi: xtivreg2 todu1joal1psh      i.jahr (subdocmel1psh = emigration), first fe cluster(id)
eststo ive_masernl1psh       : xi: xtivreg2 masernl1psh        i.jahr (subdocmel1psh = emigration), first fe cluster(id)
eststo ive_scharlachl1psh    : xi: xtivreg2 scharlachl1psh     i.jahr (subdocmel1psh = emigration), first fe cluster(id)
eststo ive_diphteriel1psh    : xi: xtivreg2 diphteriel1psh     i.jahr (subdocmel1psh = emigration), first fe cluster(id)
eststo ive_influenzaohnel1psh: xi: xtivreg2 influenzaohnel1psh i.jahr (subdocmel1psh = emigration), first fe cluster(id)
eststo ive_bronchitisl1psh   : xi: xtivreg2 bronchitisl1psh    i.jahr (subdocmel1psh = emigration), first fe cluster(id)
eststo ive_lungel1psh        : xi: xtivreg2 lungel1psh         i.jahr (subdocmel1psh = emigration), first fe cluster(id)
estout using "$maindir/Results/Tables/diseases.tex", style(tex) replace ///
    keep(subdoc*) ///
    cells(b(star fmt(3)) se(par fmt(3)) p(par([ ]) fmt(3))) ///
    stats(widstat ymean N_clust N, fmt(2 2 0 0)) ///
    starlevels(* 0.1 ** 0.05 *** 0.01) ///
    $texcode
eststo clear




/* ===================== EVENT STUDY TYPE SPECIFICATION ===================== */

// FIGURE 4

version 13
set more off
global maindir "/home/`c(username)'/Dropbox/Research/Nazis"

use "$maindir/Data/master.dta", clear
eststo clear

preserve
drop if jahr == 1927
drop if jahr == 1928
capture drop temig
bysort gname: egen temig = total(emigration)
local var temig
xtreg lntodu1joa ib1932.jahr##c.`var', fe vce(cluster id)
coefplot, vertical omitted baselevels levels(95) ///
  color(black) ylab(, nogrid) graphregion(color(white)) ///
  keep(19*.jahr#c.`var') rename(*.jahr#c.`var' = "") ///
  yline(0, lc(black) lp(longdash) lw(*0.5)) ///
  xline(4, lc(red) lp(longdash) lw(*0.5)) ///
  ylab(-0.0001(0.0001)0.0004) ///
  ciopts(recast(rcap) color(black) lw(*0.5)) ///
  legend(order(2 "Point estimate" 1 "95% Confidence interval") region(color(white))) ///
  name(event`var', replace)
graph save "$maindir/Results/Graphs/event-`var'.gph", replace
graph export "$maindir/Results/Graphs/event-`var'.eps", replace logo(off)
graph export "$maindir/Results/Graphs/event-`var'.pdf", replace logo(off)
writepsfrag "$maindir/Results/Graphs/event-`var'.eps" using "$maindir/Results/Graphs/event-`var'.tex", replace textsize(\footnotesize)
gr_edit .title.text.Arrpush "Infant mortality (ln), exposure: year x emigration"
gr_edit .title.yoffset = 3
graph save "$maindir/Results/Graphs/eventalt-`var'-title.gph", replace
eststo clear
restore

graph close _all



/* ========================= HOSPITAL INFRASTRUCTURE ======================== */

// TABLE 5

version 14
use "$maindir/Data/master.dta", clear
eststo clear
capture noisily drop if jahr == 1927
capture noisily drop jahr_1927

capture noisily program drop gmmcfiv
program define gmmcfiv
  capture drop _r*
  args depvar endvar iv addvar0 addvar1 addvar2 addvar3
  local ybegin = 1929
  quietly levelsof jahr if !missing(`depvar')
  tokenize `r(levels)'
  local ybegin = "`2'"
  /* display "`ybegin'" */
  foreach var of varlist `depvar' `endvar' `iv' `addvar0' `addvar1' `addvar2'  `addvar3' {
      // jahr_19* {
    if "`var'" != "" {
      tempvar _m`var'
      quietly egen `_m`var'' = mean(`var'), by(id)
      quietly generate _r`var' = `var' - `_m`var''
      /* quietly generate _r`var' = `var' - l1.`var' */
      /* display "`var' _r`var'" */
    }
  }
  if "`addvar0'" != "" local  _raddvar0 _r`addvar0'
  if "`addvar0'" != "" local  b2 +{b2}*_r`addvar0'
  if "`addvar1'" != "" local  _raddvar1 _r`addvar1'
  if "`addvar1'" != "" local  b3 +{b3}*_r`addvar1'
  if "`addvar2'" != "" local  _raddvar2 _r`addvar2'
  if "`addvar2'" != "" local  b4 +{b4}*_r`addvar2'
  if "`addvar3'" != "" local  _raddvar3 _r`addvar3'
  if "`addvar3'" != "" local  b5 +{b5}*_r`addvar3'
  if `ybegin' == 1929 {
    local xbeta ///
      {b1}*_r`endvar' `b2' `b3' `b4' `b5' + {bc}*(_r`endvar'-{xb:}) + {b0}    ///
      + {y29}*jahr_1929 + {y30}*jahr_1930 + {y31}*jahr_1931 + {y32}*jahr_1932 ///
      + {y33}*jahr_1933 + {y34}*jahr_1934 + {y35}*jahr_1935 + {y36}*jahr_1936 // + {pop}*lnpop
  }
  if `ybegin' == 1932 {
    local xbeta ///
        {b1}*_r`endvar' `b2' `b3' `b4' `b5' + {bc}*(_r`endvar'-{xb:}) + {b0} + {y32}*jahr_1932 ///
      + {y33}*jahr_1933 + {y34}*jahr_1934 + {y35}*jahr_1935 + {y36}*jahr_1936
  }
  display in green "Equation: `xbeta'"
  gmm                                                           ///
    (eq3: (_r`endvar' - {xb:_r`iv' _cons jahr_`ybegin'-jahr_1936})) ///
    (eq1: (_r`depvar' - (`xbeta')))                             ///
    (eq2: (_r`depvar' - (`xbeta'))*(_r`endvar' - {xb:})),       ///
    instruments(eq3: _r`iv'                                                     jahr_`ybegin'-jahr_1936) ///
    instruments(eq1: _r`endvar' `_raddvar0' `_raddvar1' `_raddvar2' `_raddvar3' jahr_`ybegin'-jahr_1936) ///
    instruments(eq2:                                                            jahr_`ybegin'-jahr_1936) ///
    winitial(unadjusted, independent) iter(100)                 ///
    igmm
  drop _r*
  estadd ysumm
end

* good infrastructure as reference
eststo clear
eststo cpive_kkho1933             :gmmcfiv infoaratel1lebgeb subdocmel1psh emigration Xkkho1933_0
eststo cpive_kkhu1933             :gmmcfiv infoaratel1lebgeb subdocmel1psh emigration Xkkhu1933_0
eststo cpive_kkhk1933             :gmmcfiv infoaratel1lebgeb subdocmel1psh emigration Xkkhk1933_0
eststo cpive_kkhf1933             :gmmcfiv infoaratel1lebgeb subdocmel1psh emigration Xkkhf1933_0
eststo cpive_betten1933rnpqt50    :gmmcfiv infoaratel1lebgeb subdocmel1psh emigration Xbetten1933rnpqt50_0
estout using "$maindir/Results/Tables/hospital-infrastructure-gmm-final.tex", style(tex) replace ///
    keep(b1:_cons b2:_cons) ///
    cells(b(star fmt(3)) se(par fmt(3)) p(par([ ]) fmt(3))) ///
    stats(ymean N_clust N, fmt(2 0 0)) ///
    starlevels(* 0.1 ** 0.05 *** 0.01)
eststo clear



